package emulator.hardware.bits;

import java.util.Observable;

import emulator.hardware.HwNumber;

public class SharedBit extends Observable {
	private boolean value;
	
	public synchronized void setValue(boolean value)
	{
		if (this.value != value)
			setChanged();
		this.value = value;
		notifyObservers(value);
	}
	
	public synchronized boolean getValue()
	{
		return value;
	}

	public static void setBits(SharedBit[] bits, int value)
	{
		HwNumber number = HwNumber.create(value);
		for (int i = 0; i < bits.length; i++)
			bits[i].setValue(number.getBit(i));
	}
	
	public static int getBits(SharedBit[] bits)
	{
		HwNumber number = HwNumber.create(0);
		for (int i = 0; i < bits.length; i++)
			number.setBit(i, bits[i].getValue());
		return (int) number.getNumber();
	}

	public synchronized void toggle() {
		value = !value;
		setChanged();
		notifyObservers(value);
	}
}
